/**
 * @author Caius
 * @description 定义获取用户详情服务Bean
 * @version 1.0
 * @since Created in 2025-02-26
 */

package com.itsense.stock.security.service;

import com.google.common.base.Strings;
import com.itsense.stock.mapper.SysUserMapper;
import com.itsense.stock.pojo.entity.SysPermission;
import com.itsense.stock.pojo.entity.SysUser;
import com.itsense.stock.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("userDetailsService")
public class LoginUserDetailService implements UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 根据用户传入的名称获取用户相关的详情信息: 用户名，密文密码， 权限集合
     * @param userName
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //通过用户名返回用户信息
        SysUser dbUser = sysUserMapper.findUserInfoByUserName(userName);
        if (dbUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //组装UserDetail对象 ： 字段：用户名， 密文密码， 权限集合
//        获取权限集合
        List<SysPermission> permissons = (List<SysPermission>) userService.getRolePermission(dbUser.getId());
        List<String> permsNameList = permissons.stream()
                .filter(permisson -> !Strings.isNullOrEmpty(permisson.getPerms()))
                .map(permission -> permission.getPerms())
                .collect(Collectors.toList());



        return null;
    }
}
